package com.leyou.item.controller;

import com.leyou.item.entity.TbCategory;
import com.leyou.item.service.CategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    private static final Logger LOGGER= LoggerFactory.getLogger(CategoryController.class);

    /**根据父节点id获取对应的子节点数据*/
    @GetMapping("list")
    public ResponseEntity<List<TbCategory>> queryListByParent(Long pid){


        List<TbCategory> tbCategories = categoryService.queryListByParent(pid);
        if(tbCategories==null || tbCategories.size()<1){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }

        return ResponseEntity.ok(tbCategories);
    }

    /*根据品牌id查询分类集合*/
    @GetMapping("categories/{bid}")
    public ResponseEntity<List<TbCategory>> queryCatogoryById(@PathVariable("bid") Long bid){
        List<TbCategory> tbCategories = categoryService.queryCategoryByBrandId(bid);
        if(tbCategories==null || tbCategories.size()<1){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(tbCategories);
    }
    /*根据分类id查询分类名称*/
    @GetMapping("names")
    public ResponseEntity<List<String>> queryCategoryNamesByCids(@RequestParam("ids") List<Long> ids){
        List<String> strings = categoryService.queryCategoryNamesByCids(ids);
        if(strings!=null){
            return ResponseEntity.ok(strings);
        }
        return ResponseEntity.notFound().build();
    }

    /**
     * 根据id获取分类对象
     * @param
     * @return
     */
    @GetMapping("category")
    public ResponseEntity<TbCategory> queryCategoryById(Long id){
        TbCategory tbCategory = categoryService.queryCategoryById(id);
        if(tbCategory==null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(tbCategory);
    }

    /*
    id: 0,
          // name: '新的节点',
          name:name,
          parentId: this.model.id,
          isParent: false,
          sort:this.model.children? this.model.children.length + 1:1

    * */
    //添加节点
    @PostMapping("addCategory")
    public ResponseEntity<Long> addCategory(TbCategory category){
        try {
            Long aLong = categoryService.addCategory(category);
            return ResponseEntity.ok(aLong);
        }catch (Exception e){
            return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);//期望失败 417
        }
    }
    //修改节点
    @PutMapping("updateCategory")
    public ResponseEntity<Void> update(TbCategory category){
        try {
            categoryService.updateCategory(category);
            return ResponseEntity.ok().build();
        }catch (Exception e){
            return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);//期望失败 417
        }
    }

    //删除节点
    @GetMapping("deleteCategory")
    public ResponseEntity<Void> deleteCategory(Long id){
        try {
            categoryService.deleteCategory(id);
            return ResponseEntity.ok().build();
        }catch (Exception e){
            return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);//期望失败 417
        }
    }


}
